<?php

/**
 * Super_Controller
 *
 * @author
 * @version
 */
class Super_Controller extends Zend_Controller_Action {

    public function preDispatch() {

        Zend_Session::start();
        $this->initTemplate();

        Zend_Layout::startMvc(array(
            'layoutPath' => '../application/layouts/scripts',
            'layout' => 'layout'));
        $view = Zend_Layout::getMvcInstance()->getView();

        $this->view->controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
        $PGM = new Application_Model_Pages();

        $this->view->labels = $PGM->select('labels', '`title`, `url`', '`active` = 1', '`is_web`,`order_by`');
        $result = array();
        $cats = $PGM->selectAss('categories', '`id`,`parent_id`,`title`, `url`, `left_menu`', '`active` = 1', '`parent_id`,`order_by`');
        $this->view->pagescats = $cats;
        if (is_array($cats) && count($cats)) {
            foreach ($cats as $cat) {
                if (empty($cat['parent_id']))
                    $result[$cat['id']] = $cat;
                else
                    $result[$cat['parent_id']]['child'][$cat['id']] = $cat;
            }
        }
        $this->view->categories = $result;
        $this->view->pages = $PGM->select('pages', '`id`,`category_id`,`title`, `url`, `top_menu`, `preview`, `in_main`, `view`', '`active` = 1', '`rating` DESC');
        $this->view->afor = $PGM->getRandomAphorisms();
        $SSTM = new Application_Model_ShortStories();
        $this->view->useful = $SSTM->getShortStoriesTitleByType('note', 20);
    }

    private function pathAllow($controller) {

        $allow[] = 'index';
        $allow[] = 'test';
        $allow[] = 'add-comment';
        //   _debug($controller,1);
        foreach ($allow as $a_controller) {
            if ($controller == $a_controller) {
                return true;
            }
        }
        return false;
    }

    static public function getDateFormat($str_date, $bNotTimeView = FALSE) {
        $result = '';
        if (!empty($str_date)) {
            $monthArray = array(1 => 'января', 2 => 'Февраля', 3 => 'марта', 4 => 'апреля', 5 => 'мая', 6 => 'июня', 7 => 'июля', 8 => 'августа', 9 => 'сентября', 10 => 'октября', 11 => 'ноября', 12 => 'декабря');

            $date = new DateTime($str_date);
            $result .= $date->format('d') . ' ';
            $result .= $monthArray[$date->format('n')] . ' ';
            $result .= $date->format('Y');
            if (!$bNotTimeView)
                $result .= ', ' . $date->format('H:i');
        }
        return $result;
    }

    function getRealIp() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }

    private function initTemplate($locale = 'ru') {
        $trans = new Zend_Translate(array(
            'adapter' => 'ini',
            'content' => APPLICATION_PATH . '/lang/site/' . $locale . '.ini',
            'locale' => $locale,
        ));
        $trans->setLocale($locale);
        Zend_Registry::set('trans', $trans);
        $this->view->assign("trans", $trans);
    }

}
